/**
 * Project hdmap
 * @author shengjialiang
 * @version v1.0
 */


#include "map_common/ObjectVector.h"

/**
 * ObjectVector implementation
 */

namespace Common
{
    CObjectVector::CObjectVector()
    {

    }

    CObjectVector::~CObjectVector()
    {

    }

    bool CObjectVector::add(Common::CObject* in_pRefObject)
    {
        bool r = false;
        this->lock();
        {
            this->mObjects.push_back(in_pRefObject);
            r = true;
        }
        this->unlock();
        return r;
    }

    int CObjectVector::size()
    {
        int r = 0;
        this->lock();
        {
            r = this->mObjects.size();
        }
        this->unlock();
        return r;
    }

    bool CObjectVector::get(std::vector<Common::CObject*>& inout_objects)
    {
        bool r = false;
        this->lock();
        {
            for (int i = 0; i < this->mObjects.size(); ++i)
            {
                inout_objects.push_back(this->mObjects.at(i));
                r = true;
            }
        }
        this->unlock();
        return r;
    }

	Common::CObject* CObjectVector::get(const std::string& in_id)
	{
		Common::CObject* r = NULL;
		this->lock();
		{
			std::vector<Common::CObject*>::iterator bitr = this->mObjects.begin();
			std::vector<Common::CObject*>::iterator eitr = this->mObjects.end();
			while (bitr != eitr)
			{
				if ((*bitr)->getID() == in_id)
				{
					r = *bitr;
				}
				++bitr;
			}

		}
		this->unlock();
		return r;
	}

    Common::CObject* CObjectVector::get(const int& in_index)
    {
        Common::CObject* r = NULL;
        this->lock();
        {
            if (this->mObjects.size() > in_index)
            {
                r = this->mObjects.at(in_index);
            }
        }
        this->unlock();
        return r;
    }

    bool CObjectVector::remove(Common::CObject* in_pObject)
    {
        bool r = false;
        this->lock();
        {
            std::vector<Common::CObject*>::iterator bitr = this->mObjects.begin();
            std::vector<Common::CObject*>::iterator eitr = this->mObjects.end();
            while (bitr != eitr)
            {
                if (*bitr == in_pObject)
                {
                    this->mObjects.erase(bitr);
                    r = true;
                    break;
                }
                ++bitr;
            }

        }
        this->unlock();
        return r;
    }

    Common::CObject* CObjectVector::remove(const int& in_index)
    {
        Common::CObject* r = NULL;
        this->lock();
        {
            if (this->mObjects.size() > in_index)
            {
                std::vector<Common::CObject*>::iterator itr = this->mObjects.begin() + in_index;
                r = *itr;
                this->mObjects.erase(itr);
            }
        }
        this->unlock();
        return r; 

    }

    bool CObjectVector::removeAndDestroy(const int& in_index)
    {
        bool r = false;
        this->lock();
        {
            if (this->mObjects.size() > in_index)
            {
                std::vector<Common::CObject*>::iterator itr = this->mObjects.begin() + in_index;
                if (*itr)
                {
                    delete *itr;
                    r = true;
                }
                this->mObjects.erase(itr);
            }
        }
        this->unlock();
        return r;

    }

    bool CObjectVector::removeAll()
    {
        bool r = false;
        this->lock();
        {
            this->mObjects.clear();
            r = true;
        }
        this->unlock();
        return r;
    }

    bool CObjectVector::removeAllAndDestroy()
    {
        bool r = false;
        this->lock();
        {
            std::vector<Common::CObject*>::iterator bitr = this->mObjects.begin();
            std::vector<Common::CObject*>::iterator eitr = this->mObjects.end();
            while (bitr != eitr)
            {
                Common::CObject* pObject = *bitr;
                if (pObject)
                {
                    delete pObject;
                }
                ++bitr;
            }
            this->mObjects.clear();
            r = true;
        }
        this->unlock();
        return r;
    }

    void CObjectVector::lock()
    {
        this->mMutex.lock();
    }

    void CObjectVector::unlock()
    {
        this->mMutex.unlock();
    }
}